<?php

namespace App\Controller\Api;

use App\Base\BaseConfig;
use App\Base\BaseController4Api;
use App\Entity\ApiUser;
use App\Model\ApiMoneyTypeModel;
use App\Model\ApiUserModel;
use App\Model\ApiUserStaticModel;
use App\Model\ApiUserStaticTypeModel;
use App\Project\ShengJinApi;
use App\Project\ShengJinModel;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;

use Symfony\Component\HttpFoundation\Request;


/**
 * @route("/api")
 */
class SampleController extends BaseController4Api
{



    public function myCurl($url,$data = []){
        $vi = "af47d8sgsdf6sdf8g15sa635f498g15h486d7s4g5";
        $time = time();
        $project = 2;
        $token = $this->createToken($vi,$time,$project);
        $head = array("vi:$vi", "t:$time", "token:$token", "project:$project",);
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $head);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
        $data = curl_exec($curl);
        curl_close($curl);
        return $data;
    }
    public function createToken($vi,$time,$project)
    {
        if ($time < time() - 300 || $time > time() + 300) {
            die("time out");
        }
        return sha1($vi . $time . $project . $time . $vi );
    }
    /**
     * @route("/test/page",name="apitestoage")
     */
    public function apitestoage()
    {
        echo serialize(array(
            ["month"=>1,"price"=>66,"save"=>23],
            ["month"=>6,"price"=>99,"save"=>23],
            ["month"=>12,"price"=>199,"save"=>23]
        ));


        $data["moneyType"] = $this->getMoneyType();
        $data["staticType"] = $this->getStaticType();
        return $this->render("Test/index.html.twig",$data);
    }

    public function createData($array)
    {
        $data = [];
        foreach ($array as $key) {
            if (!$$key = $this->getParamRequired($key)) {$$key = "";}
            $data[$key] = $$key;
        }
        return $data;
    }

    public function getMoneyType()
    {
        $ApiMoneyTypeModel = new ApiMoneyTypeModel($this->getDoctrine());
        $data = $ApiMoneyTypeModel->getByProject(2);
        $re = [];
        foreach ($data as $row) {
            $re[$row->getId()] = $row->getName();
        }
        return $re;
    }

    public function getStaticType()
    {
        $ApiMoneyTypeModel = new ApiUserStaticTypeModel($this->getDoctrine());
        $data = $ApiMoneyTypeModel->getByProject(2);
        $re = [];
        foreach ($data as $row) {
            $re[$row->getCode()] = $row->getName();
        }
        return $re;
    }

    /**
     * 创建用户
     * @route("/test/user_create",name="apitest_user_create")
     */
    public function apitest_user_create()
    {
//        $url = BaseConfig::__BASE_URL__."/api/user_create";
//        $data = array(
//            "uid"=>1,
//            "parent"=>0
//        );
//        echo $this->myCurl($url,$data);die;

        $url = BaseConfig::__BASE_URL__."/api/user_create";
        $data = $this->createData(["uid","parent"]);
        echo $this->myCurl($url,$data);die;
    }
    /**
     * 资金变动
     * @route("/test/money_add",name="apitest_money_add")
     */
    public function apitest_money_add()
    {
        $url = BaseConfig::__BASE_URL__."/api/money_add";
        $data = $this->createData(["uid","moneyType","money","code","tips"]);
        echo $this->myCurl($url,$data);die;
    }
    /**
     * 获取用户资产余额
     * @route("/test/get_user_money",name="apitest_get_user_money")
     */
    public function apitest_get_user_money()
    {
        $url = BaseConfig::__BASE_URL__."/api/get_user_money";
        $data = $this->createData(["uid"]);
        echo $this->myCurl($url,$data);die;
    }


    /**
     * 获取所有积分配置
     * @route("/test/get_money_config",name="apitest_money_config_get")
     */
    public function apitest_money_config_get()
    {
        $url = BaseConfig::__BASE_URL__."/api/get_money_config";
        $data = $this->createData(["uid"]);
        echo $this->myCurl($url,$data);die;
    }



    /**
     * 修改积分配置
     * @route("/test/set_money_config",name="apitest_money_config_set")
     */
    public function apitest_money_config_set()
    {
        $url = BaseConfig::__BASE_URL__."/api/set_money_config";
        $data = $this->createData(["id","val","tips"]);
        echo $this->myCurl($url,$data);die;
    }

    /**
     * 测试
     * @route("/test/test",name="apitest_test")
     */
    public function apitest_test()
    {
//        $mo = new ShengJinApi();
//        $data = $mo->addJfFromFriend(1,2,100);
//        var_dump($data);
//        $ApiUserModel = new ApiUserModel($this->getDoctrine());
//        $data = $ApiUserModel->getSonsTree(2,1,4);
//        var_dump($data);
//        echo json_encode($data);
//        $data = $ApiUserModel->getTeamCondition(2,1);
        $ShengJinModel = new ShengJinModel($this->getDoctrine());
        $data = $ShengJinModel->checkAndLevelUp(2,1);
        return $this->json_out(1,1,$data);
    }



}
